import { Ref } from 'vue'

// 提取公用的切换登录状态类型的逻辑

export enum LoginStateEnum {
  PASSWD_LOGIN,
  SMS_CODE_LOGIN,
  RESET_PASSWD
}

// 唯一的登录状态数据
const currentState = ref(LoginStateEnum.PASSWD_LOGIN)

export const useLoginState = () => {
  function setLoginState(state: LoginStateEnum) {
    currentState.value = state
  }

  const getLoginState = computed(() => currentState.value)

  function handleBackLogin() {
    setLoginState(LoginStateEnum.PASSWD_LOGIN)
  }

  return {
    setLoginState,
    getLoginState,
    handleBackLogin
  }
}

export const useFormValid = (formRef: Ref<any>) => {
  async function validForm() {
    const form = unref(formRef)
    if (!form) {
      return
    }
    return await form.validate()
  }

  return {
    validForm
  }
}
